package com.tq68.weixin.rules;

import java.util.Date;

import com.tq68.weixin.model.Bbs;
import com.tq68.weixin.model.CoinsLog;
import com.tq68.weixin.model.Seller;
import com.tq68.weixin.model.User;
import com.tq68.weixin.utils.DateUtils;

public class GoldCoin {
	
	//发帖等活动金币奖励数量规则
		private int ADDGOLD_NEWPOST = 2;
		
	//帖子被点赞金币奖励数量规则，梯度
		//1.业主
		private int LIKENUM_YZ1 = 5;
		private int LIKENUM_YZ2 = 10;
		private int LIKENUM_YZ3 = 20;
		
		private int ADDGOLD_LIKE_YZ1 = 1;
		private int ADDGOLD_LIKE_YZ2 = 2;
		private int ADDGOLD_LIKE_YZ3 = 3;
		
		//2.商家
		private int LIKENUM_SJ1 = 5;
		private int LIKENUM_SJ2 = 10;
		private int LIKENUM_SJ3 = 20;
		
		private int ADDGOLD_LIKE_SJ1 = 1;
		private int ADDGOLD_LIKE_SJ2 = 2;
		private int ADDGOLD_LIKE_SJ3 = 3;
		

	
	/**
	 * 发帖后调用此方法，判断是否需要奖励金币和数量，并更新。
	 * return 0-没有奖励金币, >0 - 奖励的金币数
	 */
	public int addForNewPost(int userId){
		User user = User.dao.findById(userId);
		user.setGoldcoin(user.getGoldcoin()+ADDGOLD_NEWPOST);
		user.update();
		//记录金币变更日志
		CoinsLog cl = new CoinsLog();
		cl.setUserid(userId);
		cl.setAddtime(DateUtils.toTimeStr(new Date()));
		cl.setCoins(ADDGOLD_NEWPOST);
		cl.setTtype(0);// 0--用户表 1--商家表
		cl.setRemark("发布一篇帖子");
		cl.save();
		return ADDGOLD_NEWPOST;
	}
	
	/**
	 * 帖子被点赞之后，调用此方法，判断是否需要对发帖业主或商家奖励金币数量，并更新。
	 * return 0-没有奖励金币, >0 - 奖励的金币数
	 */
	public int addforLikeSummary(int bbsId){
		int re = 0;
		Bbs bbs = Bbs.dao.findById(bbsId);
		String remark = "帖子\""+bbs.getContent().substring(0,3)+"...\"累计收到";
		if(bbs.getForumSubject()!=9){//业主发的贴
			if(bbs.getYescount()==LIKENUM_YZ1){
				re = ADDGOLD_LIKE_YZ1;
				remark += LIKENUM_YZ1;
			}else if(bbs.getYescount()==LIKENUM_YZ2){
				re = ADDGOLD_LIKE_YZ2;
				remark += LIKENUM_YZ2;
			}else if(bbs.getYescount()==LIKENUM_YZ3){
				re = ADDGOLD_LIKE_YZ3;
				remark += LIKENUM_YZ3;
			}
			if(re!=0){
				User user = User.dao.findById(bbs.getUserid());
				user.setGoldcoin(user.getGoldcoin()+re);
				user.update();
				//记录金币变更日志
				CoinsLog cl = new CoinsLog();
				cl.setUserid(bbs.getUserid());
				cl.setAddtime(DateUtils.toTimeStr(new Date()));
				cl.setCoins(re);
				cl.setTtype(0);// 0--用户表 1--商家表
				cl.setRemark(remark+"个赞");
				cl.save();
			}
			return re;
		}else{//商家发的帖
			re = 1; //商家的帖每得到一个赞，商家获得一个金币。
			/*
			if(bbs.getYescount()==LIKENUM_SJ1){
				re = ADDGOLD_LIKE_SJ1;
			}else if(bbs.getYescount()==LIKENUM_SJ2){
				re = ADDGOLD_LIKE_SJ2;
			}else if(bbs.getYescount()==LIKENUM_SJ3){
				re = ADDGOLD_LIKE_SJ3;
			}*/
			if(re!=0){
				Seller seller = Seller.dao.findById(bbs.getUserid());
				seller.setCoinCount(seller.getCoinCount()+re);
				seller.update();
				//记录金币变更日志
				CoinsLog cl = new CoinsLog();
				cl.setUserid(bbs.getUserid());
				cl.setAddtime(DateUtils.toTimeStr(new Date()));
				cl.setCoins(re);
				cl.setTtype(1);// 0--用户表 1--商家表
				cl.setRemark("优惠\""+bbs.getContent().substring(0,3)+"...\"获得一个赞");
				cl.save();
			}
			return re;
		}

	}

}
